<?php
	class Debugger
	{
		/**
		 * debug array
		 */
		private static $debug;
		
		/**
		 * logs' array
		 */
		private static $logs;
		
		/**
		 * debug array iteration
		 */
		private static $iteration = 0;	    
	    
		
		/* Logger */
		
		/**
		 * Logs the point
		 *
		 * @param string $text text to be logged
		 */
		public static function Log($name, $type = 'single')
		{
		    global $object;
		    
			//exits the logger if we do not use system logger
			if ((isset($object->config->use_system_logger)) && ($object->config->use_system_logger))
			{
				//gets the current 
				$current_mktime = microtime() + time();
				
				if (self::$iteration == 0) $previous_mktime = $current_mktime; 
				else $previous_mktime = self::$debug[self::$iteration - 1]['mktime'];
				
				//list($decsec, $sec) = explode(" ", $current_mktime);
				
				//time spent for the element
				$time_spent = ($type == 'end') ? ($current_mktime - self::$logs[$name]['start']['mktime']) : '';
				
				//total time
				if (self::$iteration == 0) $total_mktime = 0;
				else $total_mktime = $current_mktime - self::$debug[0]['mktime'];
				
				//debug information
		 		$backtrace = debug_backtrace();
		 		$debug_file = $backtrace[0]['file'];
		 		$debug_line = $backtrace[0]['line'];
		 		$debug_function = $backtrace[1]['function'];
		 		$debug_class = (isset($backtrace[1]['class'])) ? $backtrace[1]['class'] : '';
		 		
		 		//inserts the log into logs array
		 		self::$logs[$name][$type] = array
		 		(
					'mktime' => $current_mktime, 
					'diff_to_prev' => $time_spent, 
					'total_time' => $total_mktime,
					'file' => $debug_file,
					'line' => $debug_line,
					'function' => $debug_function,
					'class' => $debug_class,
		 		);	 		
		 	
				self::$iteration++;
			}
		}
		
		/**
		 * Generates logger table
		 */
		public static function DisplayLogs()
		{
		    //self::VarDump(self::$logs); return;
		    
		    global $object;
		    
			if ((isset($object->config->use_system_logger)) && ($object->config->use_system_logger))
			{
				XHTML::Style
				("
					.logTable td {font-family: verdana; font-size: 11px; border-collapse:collapse; border-width: 1px; border-style:solid; border-color: gray; padding:0px 0px 0px 3px;}
					.logHeaderRow td {background-color: #78A7C8; color: #FFFFFF; }
				");
				
				XHTML::Script('', "
					function ShowHideTag(tag_id)
					{
						element = document.getElementById(tag_id);
						if(element.style.display == 'none')
						{
							element.style.display='';
						}
						else
						{
							element.style.display='none';
						}	
					}					
				");
				
				$logTable = new Table('log');
				$logTable->SetAttribute("style", "width: 100%; border: 1px solid gray; border-collapse: collapse; ");
				$logTable->SetClass('logTable');
				
				//header row
				$headerRow = new TableRow('logHeader');
				$headerRow->SetClass('logHeaderRow');

				//name
				$headerNameCell = new TableCell();
				$headerNameCell->SetContent('Name');
				$headerRow->AppendCell($headerNameCell);
				
				//start time
				$headerStartCell = new TableCell();
				$headerStartCell->SetContent('Start');
				$headerStartCell->SetStyle("width: 200px; ");
				$headerRow->AppendCell($headerStartCell);
				
				//end time
				$headerEndCell = new TableCell();
				$headerEndCell->SetContent('End');
				$headerEndCell->SetStyle("width: 200px; ");
				$headerRow->AppendCell($headerEndCell);
				
				//spent time
				$headerSpentCell = new TableCell();
				$headerSpentCell->SetContent('Time spent');
				$headerSpentCell->SetStyle("width: 150px; ");
				$headerRow->AppendCell($headerSpentCell);
				
				//actions
				$headerActionCell = new TableCell();
				$headerActionCell->SetContent('Actions');
				$headerActionCell->SetStyle("width: 60px; ");
				$headerRow->AppendCell($headerActionCell);
				
				$logTable->AppendRow($headerRow);
				
				$rr = (self::$logs);
				
				$i = 0;
				foreach (self::$logs as $name => $log)
				{
					$fileStart = str_replace($_SERVER['DOCUMENT_ROOT'], '', str_replace('\\', '/', strtolower($log['start']['file'])));
					$fileEnd = str_replace($_SERVER['DOCUMENT_ROOT'], '', str_replace('\\', '/', strtolower($log['end']['file'])));
					$start_location_message = "<div id = 'startLocationMessage$i' style = 'display: none'><b>File:</b> {$fileStart}; <br /><b>Function:</b> {$log['start']['function']}; <br /><b>Class:</b> {$log['start']['class']}; <br /><b>Line:</b> {$log['start']['line']}</div>";
					$end_location_message = "<div id = 'endLocationMessage$i' style = 'display: none'><b>File:</b> {$fileEnd}; <br /><b>Function:</b> {$log['end']['function']}; <br /><b>Class:</b> {$log['end']['class']}; <br /><b>Line:</b> {$log['end']['line']}</div>";
					
					$row[$i] = new TableRow();
										
					$cell['name'] = new TableCell('');
					$cell['name']->SetContent($name);
					$row[$i]->AppendCell($cell['name']);
										
					$cell['start'] = new TableCell('');
					$cell['start']->SetContent(round($log['start']['total_time']*1000, 2)."<br />".$start_location_message);
					$row[$i]->AppendCell($cell['start']);
					
					$cell['end'] = new TableCell('');
					$cell['end']->SetContent(round($log['end']['total_time']*1000, 2)."<br />".$end_location_message);
					$row[$i]->AppendCell($cell['end']);
					
					$cell['spent'] = new TableCell('');
					$cell['spent']->SetContent(round($log['end']['diff_to_prev']*1000, 2));
					$row[$i]->AppendCell($cell['spent']);
					
					$cell['action'] = new TableCell('');
					$cell['action']->SetContent(XHTML::GetTag('a', "onclick = 'ShowHideTag(\"startLocationMessage$i\"); ShowHideTag(\"endLocationMessage$i\");' style = 'cursor: pointer'", 'Details...'));
					$row[$i]->AppendCell($cell['action']);
					
					$logTable->AppendRow($row[$i]);

					$i++;
				}
				$logTable->Display();
			}
		}	    
	    
		/**
		 * Prints variables' dump
		 *
		 * @param mixed $data data to be dumped
		 */
		static function VarDump($data)
		{
			echo '<pre>---------------- Variable dump started ----------------<br/>';
				var_dump($data);
			echo '---------------- Variable dump ended ----------------</pre>';
		}
	}
?>